using System.Collections.Generic;
using UnityEngine;

public class NameAndCountrySelectController : UILayer
{
	public UILabel nameLabel;

	public UICenterOnChild center;

	private int tableReposition;

	protected Transform originalSelectedFlagTransform;

	public CountrySelectButton buttonPrefab;

	public UITable table;

	public UIScrollView view;

	public bool fullScreen;

	private Dictionary<GameObject, GameConstants.Country> countryMap = new Dictionary<GameObject, GameConstants.Country>();

	private List<CountrySelectButton> buttons = new List<CountrySelectButton>();

	private bool isCentered;

	private void UpdateCountriesDisplay()
	{
		List<GameConstants.Country> countries = GameConstants.Instance.Countries;
		countryMap.Clear();
		for (int i = 0; i < countries.Count; i++)
		{
			if (i >= buttons.Count)
			{
				CountrySelectButton component = NGUITools.AddChild(table.gameObject, buttonPrefab.gameObject).GetComponent<CountrySelectButton>();
				component.name = countries[i].countryName;
				buttons.Add(component);
			}
			buttons[i].Init(countries[i]);
			buttons[i].gameObject.name = countries[i].countryName;
			countryMap.Add(buttons[i].gameObject, countries[i]);
			if (countries[i].flag == (GameConstants.Flags)GGPlayerSettings.instance.Model.flag)
			{
				originalSelectedFlagTransform = buttons[i].transform;
			}
		}
		for (int j = countries.Count; j < buttons.Count; j++)
		{
			buttons[j].gameObject.SetActive(false);
		}
		table.Reposition();
	}

	private void OnEnable()
	{
		if (fullScreen)
		{
			TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
			tableTennisMainNavigation.SetHeader(TableTennisMainNavigation.HeaderType.None);
			tableTennisMainNavigation.characterView.ShowAll();
			tableTennisMainNavigation.characterView.ShowView(CharacterDisplayView.ViewType.NameSelectView);
		}
		center = table.GetComponent<UICenterOnChild>();
		if (!GGPlayerSettings.instance.Model.shownMainMenuTutorial)
		{
			GGPlayerSettings.instance.Model.shownMainMenuTutorial = true;
			GGPlayerSettings.instance.Save();
		}
		tableReposition = 0;
		if (GGPlayerSettings.instance.Model.name != null && GGPlayerSettings.instance.Model.name != "You")
		{
			UITools.ChangeText(nameLabel, GGPlayerSettings.instance.Model.name);
		}
		else
		{
			UITools.ChangeText(nameLabel, "You");
		}
		UpdateCountriesDisplay();
		center.CenterOn(originalSelectedFlagTransform);
		isCentered = false;
	}

	private void OnDisable()
	{
		if (fullScreen)
		{
			TableTennisMainNavigation tableTennisMainNavigation = NavigationManager.instance as TableTennisMainNavigation;
		}
	}

	public override void Update()
	{
		if (isEntered && NavigationManager.instance.shouldGoBack)
		{
			OnDone();
		}
		center = table.GetComponent<UICenterOnChild>();
		if (center != null && !isCentered)
		{
			center.CenterOn(originalSelectedFlagTransform);
			isCentered = true;
		}
	}

	public void OnDone()
	{
		center.Recenter();
		GameObject centeredObject = center.centeredObject;
		if (countryMap.ContainsKey(centeredObject))
		{
			GameConstants.Country country = countryMap[centeredObject];
			GGPlayerSettings.instance.PickFlag((int)country.flag);
		}
		GGPlayerSettings.instance.SetName(nameLabel.text);
		NavigationManager.instance.Pop();
	}
}
